package com.fulinlin.generator;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.querys.MariadbQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.baomidou.mybatisplus.generator.query.SQLQuery;
import com.fulinlin.generator.config.FileConfig;
import com.fulinlin.generator.config.GenerateConfig;
import com.fulinlin.generator.entity.TableInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 *
 */
public class GenerateTest {
    public static void main(String[] args) throws IOException {
//        outDocx();
        createExcel();
    }

    private static void createExcel() throws IOException {
// 创建一个简单的Mini-Table

        List<TableInfo> rs = outData();
        String path = "C:\\Users\\mqiqe\\Desktop\\";
        // 1.创建一个工作簿。
        Workbook workbook = new HSSFWorkbook();
        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("表数据结构");
        // 3.创建行。第一行
        Row row = sheet.createRow(0);
        // 4.创建列。
        // (1,1) 第一行第一列的单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("表明");
        // (1,2) 第一行第二列的单元格
        Cell cell2 = row.createCell(1);
        cell2.setCellValue("表结构");
        int i = 1;
        for (TableInfo r : rs) {
            // 第二行。(1,0)
            Row row1 = sheet.createRow(i);
            cell = row1.createCell(0);
            cell.setCellValue(r.getTable().getName());
            cell2 = row1.createCell(1);
            cell2.setCellValue(r.getTable().getRemarks());
            i++;
        }

        // 判断文件是否存在，不存在就创建
        if (FileUtil.isEmpty(new File(path))){
            FileUtil.mkdir(path);
        }
        // 5.生成一张表。03版本的工作簿是以.xls结尾
        FileOutputStream fileOUtputStream = new FileOutputStream(path+"表结构.xls");
        // 输出
        workbook.write(fileOUtputStream);
        // 6.关闭流
        fileOUtputStream.close();
        workbook.close();
        System.out.println("生成成功");
    }

    private static void outDocx() {
        GenerateConfig generateConfig = new GenerateConfig();
        generateConfig.setDataSourceConfig(
                        new DataSourceConfig.Builder(
                                "jdbc:mariadb://127.0.0.1:3306/ssc_sjzz2",
                                "root",
                                "123456")
                                //.schema("PUBLIC")
                                .dbQuery(new MariadbQuery())
                                .keyWordsHandler(new MySqlKeyWordsHandler())
                                .databaseQueryClass(SQLQuery.class)
                                .build()
                )
                .setStrategyConfig(
                        new StrategyConfig.Builder()
                                .enableCapitalMode()
                                .enableSkipView()
                                .disableSqlFilter()
                                .build()
                )
                .setFileConfig(
                        new FileConfig.Builder()
                                .template("/templates/poi-tl.docx")
                                .writeFile("C:\\Users\\mqiqe\\Desktop\\测试.docx")
                                .build()
                );
        generateConfig.execute();
    }

    private static List<TableInfo> outData() {
        GenerateConfig generateConfig = new GenerateConfig();
        generateConfig.setDataSourceConfig(
                        new DataSourceConfig.Builder(
                                "jdbc:mariadb://127.0.0.1:3306/ssc_sjzz2",
                                "root",
                                "123456")
                                //.schema("PUBLIC")
                                .dbQuery(new MariadbQuery())
                                .keyWordsHandler(new MySqlKeyWordsHandler())
                                .databaseQueryClass(SQLQuery.class)
                                .build()
                )
                .setStrategyConfig(
                        new StrategyConfig.Builder()
                                .enableCapitalMode()
                                .enableSkipView()
                                .disableSqlFilter()
                                .build()
                )
                .setFileConfig(
                        new FileConfig.Builder()
                                .template("/templates/poi-tl.docx")
                                .writeFile("C:\\Users\\mqiqe\\Desktop\\测试.docx")
                                .build()
                );
        return generateConfig.queryTableList();
    }
}
